<template>
	<div class="container">
		<van-nav-bar
			title="授权结果"
			left-text="返回"
			left-arrow
			@click-left="onClickLeft"
		/>
		<div class="content">
			<div
				class="success_box"
				v-if="isSuccess === 'yes'"
			>
				<img
					src="~img/authorize/success.png"
					alt=""
				/>
				<h3>授权成功</h3>
			</div>
			<div
				class="success_box"
				v-if="!isSuccess"
			>
				<!-- <img src="~img/authorize/success.png" alt="" /> -->
				<van-loading type="spinner" />
				<h3 style="margin-top: 20px">正在授权...</h3>
			</div>
			<div
				class="success_box"
				v-if="isSuccess === 'no'"
			>
				<img
					src="~img/authorize/error.png"
					alt=""
				/>
				<h3>授权失败</h3>
			</div>
			<div class="btn_box">
				<van-button
					type="info"
					block
					@click="backClick"
					v-if="isSuccess"
				>确定</van-button>
			</div>
		</div>
	</div>
</template>

<script>
import { generateAuthorization, insertMes } from '@/interfaces/authorize';
export default {
	data () {
		return {
			isSuccess: '',
			times: 0,
		};
	},
	created () {
		if (this.getQueryStringhash('code') == 0) {
			// this.verifyInfo();
			// 查询授权结果
			this.isSuccess = 'yes';
			const wfParams = JSON.parse(localStorage.getItem('wfForm'));
			const zxForm = JSON.parse(localStorage.getItem('zxForm'));
			this.saveNeedForms(wfParams, zxForm);
			// this.pollApi();
		} else {
			this.isSuccess = 'no';
			// console.log(this.getQueryStringhash('code') == 0);
		}
	},
	methods: {
		async verifyInfo () {
			// this.$toast({
			// 	message: '授权中...',
			// 	forbidClick: true,
			//   loadingType: 'spinner',
			//   duration: 0
			// });
			// 1、微风的登录
			const wfParams = JSON.parse(localStorage.getItem('wfForm'));
			console.log(wfParams, 'wfParams');
			const wfAu = await this.getBasicInfo(wfParams.taxpayerId);
			// 2、微风的授权
			// wfParams.channelCode = '51e2s';
			// const res = await this.authorizeLogin(wfParams);
			// console.log(res, '授权结果');
			// if (res.code == '200') {
			// 	// 查询授权结果
			// 	this.pollApi();
			// } else {
			// 	this.$toast(res.message);
			// 	this.isSuccess = 'no';
			// }
		},
		async pollApi () {
			this.times++;
			const wfParams = JSON.parse(localStorage.getItem('wfForm'));
			// 3、轮询查询授权的结果
			const resultWf = await this.getAuthrizeResult();
			console.log(resultWf, 'resultWf');
			if (resultWf.code == '200') {
				this.isSuccess = 'yes';
				// 4、征信的授权
				const zxForm = JSON.parse(localStorage.getItem('zxForm'));
				// 存储数据
				this.saveNeedForms(wfParams, zxForm);
				let signDateArr = zxForm.startauthdate.split('-');
				let asDateArr = zxForm.authdate.split('-');
				const zxParams = {
					enterpriseName: zxForm.corpName, // 企业名称
					enterpriseIdCode: zxForm.corpCredit, // 企业统一信用代码
					basicBankOfAccount: zxForm.bankName, // 基本开户行
					legalPerson: zxForm.legalName, // 机构法人
					legalPersonIdNumber: zxForm.lagelPersonId, // 法人身份证号
					// 开始日期
					signDateOfYear: signDateArr[0],
					signDateOfMonth: signDateArr[1],
					signDateOfDay: signDateArr[2],
					// 结束日期
					asOfTheDateYear: asDateArr[0],
					asOfTheDateMonth: asDateArr[1],
					asOfTheDateDay: asDateArr[2],
					orderId: this.getQueryStringhash('orderNo'), // 人脸订单号
					// 需改为苏州银行给你传的标识
					// wsUser: viewinfo.originBankid,
					enquiryOfOrganizationName: zxForm.authobject, // 授权对象名称
				};
				console.log(zxParams, 'zxParams');
				const resZx = await generateAuthorization(zxParams);
				console.log(resZx, 'resZx');
			} else {
				// 再次查询
				if (this.times > 20) {
					this.isSuccess = 'no';
				} else {
					const _this = this;
					setTimeout(() => {
						_this.pollApi();
					}, 1000);
				}
			}
		},
		// 保存数据
		saveNeedForms (wfForm, zxForm) {
			const params = {
				corpName: zxForm.corpName,
				corpCredit: zxForm.corpCredit,
				khh: zxForm.bankName,
				operManName: zxForm.legalName,
				cerno: zxForm.lagelPersonId,
				phone: '',
				granter: zxForm.authobject,
				startDate: zxForm.startauthdate,
				endDate: zxForm.authdate,
				// taxationMes: JSON.stringify(wfForm)
				taxAccount: wfForm.username,
				taxPsw: wfForm.password,
				createTime: zxForm.startauthdate,
				filePath: '',
			};
			insertMes(params);
		},
		// 获取授权基本信息
		getBasicInfo (id) {
			const params = {
				taxpayerId: id,
				channelCode: '51e2s',
			};
			console.log(params, '获取授权基本信息');
			return new Promise((resolve, reject) => {
				authSdk.getBasicInfo(params).then((res) => {
					resolve(res);
				});
			});
		},
		// 微风授权登录
		authorizeLogin (params) {
			return new Promise((resolve, reject) => {
				authSdk.authorizeLogin(params).then((res) => {
					resolve(res);
				});
			});
		},
		// 获取授权结果
		getAuthrizeResult (params = {}) {
			return new Promise((resolve, reject) => {
				authSdk.getAuthrizeResult(params).then((res) => {
					resolve(res);
				});
			});
		},
		onClickLeft () {
			this.$router.push('/authorize');
		},
		backClick () {
			this.$router.push('/authorize');
		},
		// 有#的情况
		getQueryStringhash (name) {
			const queryStr = String(window.location).split('?')[1] || '';
			name = encodeURIComponent(name);
			let arr = new RegExp('(^|&)' + name + '=([^&]*)(&|$)').exec(queryStr);
			if (arr) {
				return RegExp.$2;
			} else {
				return '';
			}
		},
	},
};
</script>

<style lang="less" scoped>
.content {
	// width: 300px;
	text-align: center;
	color: black;
	position: absolute;
	top: 300px;
	left: 0;
	right: 0;
	bottom: 0;
	margin: auto;

	.success_box {
		img {
			width: 76px;
			height: 76px;
		}

		h3 {
			color: #2c2c2c;
			font-size: 19px;
			font-weight: 500;
			text-align: center;
		}
	}

	.btn_box {
		padding: 100px 120px 0;
	}
}</style>
